/*
 * @Author: JohnYang
 * @Date: 2020-09-27 21:29:39
 * @LastEditors: Zhang Yingying
 * @LastEditTime: 2020-11-23 22:29:52
 */

import { ConfigKey } from "@/config";

function getStorage(prop) {
  let storage = sessionStorage.getItem(ConfigKey);
  try {
    storage = JSON.parse(storage);
  } catch (exp) {
    console.error(exp);
  }
  if (storage && typeof storage !== "object") {
    storage = {};
  }
  return storage[prop] || [];
}

function getMapping(prop) {
  let data = getStorage(prop);
  return Array.isArray(data)
    ? data.map(x => {
        return {
          ...x,
          label: x.name,
          value: x.id
        };
      })
    : [];
}

export const brandMap = new Map();

export const VehicleModelMap = {};

export const BrandMap = {};

let brands = getMapping("brand");
brands.forEach(item => {
  BrandMap[item.id] = item;
  brandMap.set(
    item.id,
    item.vehicleModel.map(x => {
      VehicleModelMap[x.id] = x;
      return {
        label: x.name,
        value: x.id
      };
    })
  );
});

export const VehiclePropertyEnumsMap = {};
let vehicleProperties = getMapping("vehicleProperty");
vehicleProperties.forEach(item => {
  VehiclePropertyEnumsMap[item.value] = item;
});

export const ProductEnumsMap = {};
let productEnums = getMapping("qualityProduct");
productEnums.forEach(item => {
  ProductEnumsMap[item.value] = item;
});

export const CertificateEnumsMap = {};
let certificateTypes = getMapping("certificateType");
certificateTypes.forEach(item => {
  CertificateEnumsMap[item.value] = item;
});

export const ManufacturerProtectTimeEnumsMap = {};
let qualityProtectTimes = getMapping("qualityProtectTime");
qualityProtectTimes.forEach(item => {
  ManufacturerProtectTimeEnumsMap[item.value] = item;
});

export const ExtendTimeSolutionEnumsMap = {};
let extendTimes = getMapping("extendTime");
extendTimes.forEach(item => {
  ExtendTimeSolutionEnumsMap[item.value] = item;
});

export const CalcMethodEnumsMap = {};
let calcMethods = getMapping("calcMethod");
calcMethods.forEach(item => {
  CalcMethodEnumsMap[item.value] = item;
});

export const QualitySolutionEnumsMap = {};
let qualityProtectPlans = getMapping("qualityProtectPlan");
qualityProtectPlans.forEach(item => {
  QualitySolutionEnumsMap[item.value] = item;
});

export const GearBoxEnumsMap = {};
let gearBoxes = getMapping("gearBox");
gearBoxes.forEach(item => {
  GearBoxEnumsMap[item.value] = item;
});

export const RegionMap = {};
let regions = getMapping("region");
regions.forEach(item => {
  RegionMap[item.value] = item;
});

export const StorageMixin = {
  methods: {
    getCarsByBrand(brand) {
      return brandMap.get(brand) || [];
    }
  },
  computed: {
    brands() {
      return getMapping("brand");
    },
    calcMethods() {
      return getMapping("calcMethod");
    },
    certificateTypes() {
      return getMapping("certificateType");
    },
    extendPlans() {
      return getMapping("extendPlan");
    },
    extendTimes() {
      return getMapping("extendTime");
    },
    gearBoxes() {
      return getMapping("gearBox");
    },
    qualityProducts() {
      return getMapping("qualityProduct");
    },
    qualityProtectPlans() {
      return getMapping("qualityProtectPlan");
    },
    qualityProtectTimes() {
      return getMapping("qualityProtectTime");
    },
    regions() {
      return getMapping("region");
    },
    vehicleProperties() {
      return vehicleProperties;
    },
    extendPlanEr1() {
      return getMapping("extendPlanEr");
    },
    extendPlan1() {
      return getMapping("extendPlan");
    }
  }
};
